package com.check.operation.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.R;
import com.check.operation.model.Inpatient;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Operation;
import com.check.operation.model.RequestUse;
import com.check.operation.model.Ssroom;
import com.check.operation.model.Sysdiclist;
import com.check.operation.model.User;
import com.check.operation.util.PlaySoundPool;
import com.szj.util.HttpUtil;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SshdAdapter extends BaseAdapter implements OnItemSelectedListener,OnClickListener{
	private LayoutInflater layoutInflater;
	private List<Operation> cws;
	private ArrayAdapter<Ssroom> ssfjop;
	private  ArrayAdapter<Sysdiclist> ssbwop;
	private User user;
	private Sysdiclist ssbwdic;
	private Ssroom ssroom;
	private List<Ssroom> ssroms;
	private List<Sysdiclist> sysdiclists;
	PlaySoundPool playSoundPool;
	
	public SshdAdapter(LayoutInflater layoutInflater,List<Ssroom> ssroms,List<Sysdiclist> sysdiclists,User user) {
		super();
		this.layoutInflater = layoutInflater;
	    this.ssfjop = new ArrayAdapter<Ssroom>(layoutInflater.getContext(), android.R.layout.simple_spinner_item); 
	    this.ssbwop = new ArrayAdapter<Sysdiclist>(layoutInflater.getContext(), android.R.layout.simple_spinner_item); 
	    ssfjop.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
	    ssbwop.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    ssfjop.addAll(ssroms);
	    ssbwop.addAll(sysdiclists); 
	    this.user=user;
	    this.ssroms=ssroms;
	    this.sysdiclists=sysdiclists;
	    playSoundPool=new PlaySoundPool(layoutInflater.getContext());
	    playSoundPool.loadSfx(R.raw.ok, 1);
		playSoundPool.loadSfx(R.raw.error, 2);
	}


	@Override
	public int getCount() {
		return cws.size();
	}

	@Override
	public Operation getItem(int position) {
		return cws.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		Operation operation=getItem(position);
		ViewHolder holder=null;
		
		if(view==null){
			view=layoutInflater.inflate(R.layout.sshdxx, null);
			holder=new ViewHolder();
			holder.ssmc=(TextView) view.findViewById(R.id.ssmc);
			holder.sqzd=(TextView) view.findViewById(R.id.sqzd);
			holder.zdys=(TextView) view.findViewById(R.id.zdys);
			holder.sssq=(TextView) view.findViewById(R.id.sssq);
			
			holder.ssfj=(Spinner) view.findViewById(R.id.ssfj);
			holder.ssfj.setAdapter(ssfjop);
			holder.ssfj.setOnItemSelectedListener(this);
			
			holder.ssbw=(Spinner) view.findViewById(R.id.ssbw);
			holder.ssbw.setAdapter(ssbwop);
			holder.ssbw.setOnItemSelectedListener(this);
			
			holder.hd=(Button) view.findViewById(R.id.hd);
			holder.hd.setOnClickListener(this);
			//holder.sstab=(TableLayout) view.findViewById(R.id.sstab);
			view.setTag(holder);
		}else{
			holder=(ViewHolder) view.getTag();
		}
		holder.hd.setTag(holder);
		holder.operation=operation;
		
		holder.ssmc.setText(operation.order_name);
		holder.sqzd.setText(operation.op_beforediag);
		holder.zdys.setText(operation.emp_name);
		holder.sssq.setText(operation.op_datetime);
		
		
		return view;
	}
	
	static class ViewHolder{
		TextView ssmc;
		TextView sqzd;
		TextView zdys;
		TextView sssq;
		Spinner ssfj;
		Spinner ssbw;
		Button hd;
		TableLayout sstab;
		Operation operation;
		int hdcs;//核对次数 
	}

	public List<Operation> getCws() {
		return cws;
	}

	public void setCws(List<Operation> cws) {
		this.cws = cws;
	}


	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
		if(arg0.getId()==R.id.ssfj){
			ssroom=this.ssroms.get(arg2);
		}else if(arg0.getId()==R.id.ssbw){
			ssbwdic=this.sysdiclists.get(arg2);
		}
		
	}


	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		ssroom=null;
	}


	@Override
	public void onClick(View v) {
		final Button butt=(Button) v;
		butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.紫色));
		
	   String action="process/process";
 	   final RequestUse requestUse=new RequestUse();
 	   
 	   final ViewHolder viewHolder=(ViewHolder) v.getTag();
 	   Operation operation=viewHolder.operation;
 	   
 	   requestUse.user=this.user;
 	   Map<String,Object> params1=new HashMap<String, Object>();
 	   params1.put("surgicNo", operation.surgic_no);
 	   params1.put("processType", "j");
 	   params1.put("position", ssbwdic.dic_code);
 	   params1.put("room",ssroom.code);
 	   requestUse.request=params1;
 	   String rs=HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
 	   MessagRe<Inpatient> mereturn=JSONObject.parseObject(rs, new TypeReference<MessagRe<Inpatient>>(){});
 	   if("success".equals(mereturn.message.code)){
 		   Toast.makeText(layoutInflater.getContext(), mereturn.message.text, Toast.LENGTH_SHORT).show();
 		   butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.绿色));
 		   viewHolder.hdcs=0;
 		   playSoundPool.play(1, 0);
 	   }else{
 		   butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.红色));
 		   Toast.makeText(layoutInflater.getContext(), mereturn.message.text+viewHolder.hdcs, Toast.LENGTH_SHORT).show();
 		   viewHolder.hdcs++;
 		   if(viewHolder.hdcs>=4){
 				new AlertDialog.Builder(layoutInflater.getContext()).setTitle("是否需要强制执行？").setPositiveButton("是",
 				       new DialogInterface.OnClickListener() {
 				           @Override
 				           public void onClick(DialogInterface dialog, int which) {
 				        	  viewHolder.hdcs=0;
 				        	  String qzaction="process/executecontinue";
 				        	  String qzrs=HttpUtil.postJSON(qzaction, JSONObject.toJSONString(requestUse));
 				        	  MessagRe<Inpatient> qzmereturn=JSONObject.parseObject(qzrs, new TypeReference<MessagRe<Inpatient>>(){});
 				        	  if("success".equals(qzmereturn.message.code)){
 				        		   playSoundPool.play(1, 0);
 				        		 Toast.makeText(layoutInflater.getContext(), qzmereturn.message.text, Toast.LENGTH_SHORT).show();
 				        		butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.黄色));
 				        	  }else{
 				        		 playSoundPool.play(2, 0);
 				        		 Toast.makeText(layoutInflater.getContext(), qzmereturn.message.text+"联系信息科！", Toast.LENGTH_SHORT).show();
 				        	  }
 				           }
 				       }).setNegativeButton("否",
 				       new DialogInterface.OnClickListener() {
 				           @Override
 				           public void onClick(DialogInterface dialog, int which) {
 				        	  viewHolder.hdcs=0;
 				           }
 				       }).show();
 		   }
 		   
 	   }
	}


}
